package com.yc.web.servlets;

import com.yc.OrderBiz;
import com.yc.bean.Resorder;
import com.yc.bean.Resuser;
import com.yc.model.CartItem;
import com.yc.model.JsonModel;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author zwei
 * @create 2024-06-13 19:05
 */
@WebServlet("/cust.action")
public class CustServlet extends BaseServlet{
    protected void confirmOrder(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, InstantiationException, IllegalAccessException, IOException, SQLException {
        JsonModel jm=new JsonModel();
        //取参
        Resorder resorder=super.parseObjectFromRequest(req,Resorder.class);
        HttpSession session=req.getSession();
        Resuser resuser= (Resuser) session.getAttribute("resuser");

        Map<Integer, CartItem>cart= (Map<Integer, CartItem>) session.getAttribute("cart");
        Set<CartItem>cartItems=new HashSet<>(cart.values());
        OrderBiz orderBiz=new OrderBiz();
        int result=orderBiz.order(resorder,cartItems,resuser);
        jm.setCode(1);
        session.removeAttribute("cart");
        super.writeJson(jm,resp);
    }
}
